package org.jeecg.modules.zhima.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.jeecg.modules.zhima.admin.entity.ZmPayOrder;
import org.jeecg.modules.zhima.admin.entity.ZmPlayer;
import org.jeecg.modules.zhima.admin.service.IZmPayOrderService;
import org.jeecg.modules.zhima.center.enums.PayStatus;
import org.jeecg.modules.zhima.portal.service.IPayOrderService;
import org.jeecg.modules.zhima.portal.service.IPayService;
import org.jeecg.modules.zhima.portal.weixin.WechatService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class PayOrderServiceImpl implements IPayOrderService {

    @Autowired
    private IZmPayOrderService zmPayOrderService;
    @Autowired
    private IPayService payService;
    @Autowired
    private WechatService wechatService;

    @Override
    public Boolean updateById(ZmPayOrder zmPayOrder) {
        return zmPayOrderService.updateById(zmPayOrder);
    }

    @Override
    public ZmPayOrder getByGamePlayerId(String gamePlayerId) {
        return zmPayOrderService.getOne(new LambdaQueryWrapper<ZmPayOrder>().eq(ZmPayOrder::getGamePlayerId, gamePlayerId).ge(ZmPayOrder::getStatus, PayStatus.WAIT_PAY.CODE));
    }

    @Override
    public ZmPayOrder getByOrderSn(String orderSn) {
        return zmPayOrderService.getOne(new LambdaQueryWrapper<ZmPayOrder>().eq(ZmPayOrder::getOrderSn, orderSn).ge(ZmPayOrder::getStatus, PayStatus.WAIT_PAY.CODE));
    }

    @Override
    public boolean removeAndRefund(String gamePlayerId) {
        ZmPayOrder zmPayOrder = this.getByGamePlayerId(gamePlayerId);
        if (zmPayOrder == null){
            return false;
        }
        if (zmPayOrder.getStatus().equals(PayStatus.PAY_SUCCESS.CODE)){
            //退款
            payService.refund(zmPayOrder.getOrderSn(), zmPayOrder.getPayAmount().multiply(new BigDecimal(100)).intValue()+"");
        }
        //删除订单记录
        zmPayOrder.setStatus(-1);
        return zmPayOrderService.updateById(zmPayOrder);
    }



    @Override
    public boolean payCallback(String orderSn) {
        ZmPayOrder zmPayOrder = this.getByOrderSn(orderSn);
        if (zmPayOrder == null){
            return false;
        }
        if (zmPayOrder.getStatus().equals(PayStatus.WAIT_PAY.CODE)){
            //付款成功
            zmPayOrder.setStatus(PayStatus.PAY_SUCCESS.CODE);
            return zmPayOrderService.updateById(zmPayOrder);
        }
        return false;
    }

    @Override
    public boolean checkPayStatusRemote(String gamePlayerId) {
        ZmPayOrder zmPayOrder = this.getByGamePlayerId(gamePlayerId);
        if (ObjectUtils.isNotEmpty(zmPayOrder)){
            if (zmPayOrder.getStatus().equals(PayStatus.WAIT_PAY.CODE)){
                boolean flag = wechatService.check(zmPayOrder.getOrderSn());
                if (flag){
                    this.payCallback(zmPayOrder.getOrderSn());
                }
                return flag;
            }
            return zmPayOrder.getStatus().equals(PayStatus.PAY_SUCCESS.CODE);
        }
        return false;
    }

    @Override
    public boolean checkPayStatus(String gamePlayerId) {
        ZmPayOrder zmPayOrder = this.getByGamePlayerId(gamePlayerId);
        if (ObjectUtils.isNotEmpty(zmPayOrder)){
            return zmPayOrder.getStatus().equals(PayStatus.PAY_SUCCESS.CODE);
        }
        return false;
    }
}
